﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Hotel
{
    public partial class Reservations : Form
    {
        public Reservations()
        {
            InitializeComponent();
        }
        CNDatabase cnDb = new CNDatabase();
        DateTime addDay = DateTime.Today.AddDays(1);

        private void Reservations_Load(object sender, EventArgs e)
        {
            txtStar.Value = addDay;
            txtEnd.Value = addDay;
            LbCurrentDay.Text = "Today is " + DateTime.Today.ToString("dd/MM/yyyy");
        }
        
        private void checkVali()
        {
            DateTime starDay = DateTime.Parse(txtStar.Text);
            DateTime endDay = DateTime.Parse(txtEnd.Text);
            DateTime currentDay = DateTime.Today;
            //
            if (starDay > endDay)
            {
                MessageBox.Show("Start date must be earlier than the end date", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txtStar.Value = addDay;
                txtEnd.Value = addDay;
            } 
                if (starDay == currentDay)
                {
                    MessageBox.Show("No reservations today, you must set earlier than 1 day", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    txtStar.Value = addDay;
                    txtEnd.Value = addDay;
                }
                    if (starDay < currentDay)
                    {
                        MessageBox.Show("No reservations the previous day", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        txtStar.Value = addDay;
                        txtEnd.Value = addDay;
                    }      
            else
            {
                cnDb.connect();
                SqlCommand cmd = new SqlCommand("sp_CheckAvailable", cnDb.cn);
                cmd.CommandText = "sp_CheckAvailable";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@StarDay", starDay);
                cmd.Parameters.Add("@EndDay", endDay);
                DataTable tb = new DataTable();
                SqlDataAdapter adt = new SqlDataAdapter(cmd);
                adt.Fill(tb);
                grvRoom.DataSource = tb;
                grvRoom.Columns[1].HeaderText = "IDCode";
                grvRoom.Columns[2].HeaderText = "Room Name";
                grvRoom.Columns[3].HeaderText = "Room Type";
                grvRoom.Columns[4].HeaderText = "Price/Room";
                cnDb.closeConnect();
            }
        }

        private void btCheck_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = true;
            groupBox2.Visible = true;
            checkVali();
        }

        private void updateCustomer()
        {
            try
            {
                DateTime birth = DateTime.Parse(txtBirth.Text);
                cnDb.connect();
                SqlCommand cmd = new SqlCommand("sp_updateCustomer", cnDb.cn);
                cmd.CommandText = "sp_updateCustomer";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Fullname", txtName.Text);
                cmd.Parameters.AddWithValue("@IdentificationNumber", txtIdentify.Text);
                cmd.Parameters.AddWithValue("@BirthDay", birth);
                cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
                cmd.Parameters.AddWithValue("@PhoneNumber", txtPhone.Text);
                cmd.ExecuteNonQuery();
                cnDb.closeConnect();
            }
            catch (Exception)
            {
                MessageBox.Show("Please enter complete customer's information", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
        
        private void booking(string type, string messengerSuccess)
        {// Type 1. CheckIn 2. Reservation
            DataGridViewRow row = new DataGridViewRow();
            int numberRoom = 0;
            for (int i = 0; i < grvRoom.Rows.Count; i++)
            {
                row = grvRoom.Rows[i];
                if (Convert.ToBoolean(row.Cells[0].Value) == true)
                {
                    numberRoom++;
                }
            }
            if (numberRoom == 0)
                MessageBox.Show("Please select room, at least 1 room !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            else
            {
                //BOOKING + add customer
                //add customer
                updateCustomer();
                //add room + day booking -->Log 
                DateTime end = DateTime.Parse(txtEnd.Text);
                DateTime star = DateTime.Parse(txtStar.Text);
                cnDb.connect();
                SqlCommand cmd = new SqlCommand("sp_Booking", cnDb.cn);
                cmd.CommandText = "sp_Booking";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@PhoneNumber", txtPhone.Text);
                cmd.Parameters.AddWithValue("@IdentificationNumber", txtIdentify.Text);
                cmd.Parameters.AddWithValue("@TypeBooking", type);
                cmd.ExecuteNonQuery();
                cnDb.closeConnect();
                //DETAIL BOOKING
                string id = cnDb.getOneData("select max(IDBookingLog) from BookingLog").ToString();
                //list room booking
                for (int i = 0; i < grvRoom.Rows.Count; i++)
                {
                    row = grvRoom.Rows[i];
                    if (Convert.ToBoolean(row.Cells[0].Value) == true)
                    {//select checkbox
                        string idRoom = Convert.ToString(row.Cells[1].Value);
                        //List
                        //string qr = "INSERT INTO ListRoomBooking(IDBookingLog,IDRoom)VALUES('" + id + "', '" + idRoom + "')";
                        string qr = "INSERT INTO ListRoomBooking(IDBookingLog,IDRoom,StartDay,EndDay, Cost) VALUES('"
                            + id + "', '" + idRoom + "', convert(datetime,'" + txtStar.Text + "', 103), convert(datetime,'" + txtEnd.Text + "', 103), '0')";
                        cnDb.xlExecuteNonQuery(qr);
                    }
                }
                checkVali();
                MessageBox.Show(messengerSuccess);

            }
        }

        private void btReservation_Click(object sender, EventArgs e)
        {
            if (txtPhone.Text == "" || txtName.Text == "" || txtIdentify.Text == "" || txtAddress.Text == "")
                MessageBox.Show("Please enter complete customer's information", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            else
                booking("Reservation", "Congratulations! Reservations successfully ");
        }

        private void lbClear_Click(object sender, EventArgs e)
        {
            txtPhone.Text = "";
            txtName.Text = "";
            txtBirth.Text = "01/01/1991";
            txtIdentify.Text = "";
            txtAddress.Text = "";
        }

        private void txtIdentify_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Enter only numeric values in this field.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                e.Handled = true;
            }
        }

       
    }
}
